package com.ctshk.rpc.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.goods.dto.CommodityDTO;
import com.ctshk.rpc.goods.dto.CommodityDTO2;
import com.ctshk.rpc.goods.dto.htmlFive.*;
import com.ctshk.rpc.goods.entity.*;
import com.ctshk.rpc.goods.mapper.*;
import com.ctshk.rpc.goods.req.conmodity.*;
import com.ctshk.rpc.goods.service.IHtmlFiveCommodityService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * H5端商品分类 服务实现类
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-08
 */
@Slf4j
@DubboService
public class HtmlFiveCommodityServiceImpl implements IHtmlFiveCommodityService {

	@Autowired
	private CommodityClassificationMapper classificationMapper; // 商品规格分类mapper

	@Autowired
	private CommodityMapper commodityMapper; // 商品mapper

	@Autowired
	private CommodityAttributeMapper commodityAttributeMapper;

	@Autowired
	private CommoditySalesAreaMapper commoditySalesAreaMapper;

	@Autowired
	private CommodityKeyWordMapper keyWordMapper; // 商品關鍵字mapper

	@Autowired
	private CommodityLabelMapper labelMapper; // 商品標籤mapper

	@Autowired
	private CommodityRecommendMapper recommendMapper; // 商品推薦mapper

	@Autowired
	private CommodityFileMapper fileMapper; // 商品文件mapper

	@Autowired
	private CommodityCostMapper costMapper; // 商品成本mapper

	@Autowired
	private CommodityCostAttributeMapper costAttributeMapper; // 商品成本表属性mapper

	@Autowired
	private ShoppingCommoditySpecificationsNameMapper specificationsNameMapper; // 商城商品规格名称mapper

	@Autowired
	private ShoppingCommoditySpecificationsMapper specificationsMapper; // 商城商品规格mapper

	@Autowired
	private ShoppingCommoditySpecificationsAttributeMapper specificationsAttributeMapper; // 商城商品规格屬性mapper

	@Autowired
	public RedisTemplate<String, String> redisTemplate; // 后续单个商品详情查询，第一次查库，放到redis缓存，下次取缓存数据

	@Override
	public List<HtmlFiveCommodityClassificationAllLevelListDTO> findClassificationByAllLevel(LocalDateTime now) {
		// 获取一级/二级分类对象集合
		List<HtmlFiveCommodityClassificationAllLevelListDTO> dbLevelOneData = classificationMapper.findHFiveClassificationByLevelOne(now);
		List<HtmlFiveCommodityClassificationLevelTwoDTO> dbLevelTwoData = classificationMapper.findHFiveClassificationByLevelTwo(now);

		// 二级分类父级ID进行分组，用于参数组装
		Map<String, List<HtmlFiveCommodityClassificationLevelTwoDTO>> levelTwo = dbLevelTwoData.stream().collect(Collectors.groupingBy(levelOjb -> levelOjb.getParentId().toString()));

		// 循环一级分类，根据ID作为二级分类父级ID取值，进行归类
		for (HtmlFiveCommodityClassificationAllLevelListDTO levelOne : dbLevelOneData) {
			levelOne.setLevelTwo(null);
			if (levelTwo.containsKey(levelOne.getId().toString())) {
				List<HtmlFiveCommodityClassificationLevelTwoDTO> levelTwos = levelTwo.get(levelOne.getId().toString());
				levelOne.setLevelTwo(levelTwos);
			}
		}
		return dbLevelOneData;
	}

	@Override
	public JSONObject findCommodityDetailedByCommodityId(Long commodityId) {
		log.info("【中旅商城--审批中心--根据ID查询商品明细--start】，入参为:{}",commodityId);
		if (null == commodityId) {
			throw new BusinessException(SystemError.SYS_402);
		}
		// 定义明细key名称前缀，用于redis缓存
		String keyName = "commodityDetailed_id_";
//		String redisCommodityDetailed = redisTemplate.opsForValue().get(keyName+commodityId);
//		// 如果redisCommodityDetailed不为空，则取缓存中的数据进行返回
//		if (StringUtils.isNotBlank(redisCommodityDetailed)){
////			 redisTemplate.delete(keyName+commodityId); // 本地测试，取完缓存，删除缓存数据，用于本地测试
//			return JSON.parseObject(redisCommodityDetailed); // 转换成JSON串返回
//		}

		// 根据入参ID，获取商品主表数据
		CommodityDTO commodityDTO = commodityMapper.findCommodityByCommodityIdSingle(commodityId);
		log.info("【中旅商城--审批中心--根据ID查询商品明细--获取商品主表数据】,返参为:{}",JSON.toJSONString(commodityDTO));
		if(commodityDTO != null){

			// 查询商品属性
			CommodityAttribute commodityAttribute = commodityAttributeMapper.findCommodityAttributeByCommodityId(commodityDTO.getId());
			log.info("【中旅商城--审批中心--根据ID查询商品明细--查询商品属性】,返参为:{}",JSON.toJSONString(commodityAttribute));
			CommodityAttributeReq commodityAttributeReq = new CommodityAttributeReq();
			if(commodityAttribute != null){
				BeanUtils.copyProperties(commodityAttribute, commodityAttributeReq);
				commodityDTO.setCommodityAttributeReq(commodityAttributeReq);
			}

			// 查询属性数据
			List<CommodityCost> commodityCostList = costMapper.findCommodityCostListByCommodityId(commodityId);
			log.info("【中旅商城--审批中心--根据ID查询商品明细--查询属性数据】,返参为:{}",JSON.toJSONString(commodityCostList));
			if(commodityCostList != null && commodityCostList.size() > 0) {
				List<CommodityCostSingleReq> commodityCostReqList = new ArrayList<>();
				commodityCostList.stream().forEach(item -> {
					CommodityCostSingleReq commodityCostSingleReq = new CommodityCostSingleReq();
					BeanUtils.copyProperties(item, commodityCostSingleReq);
					// 查询对应的属性信息
					List<CommodityCostAttribute> commodityCostAttributes = costAttributeMapper.findCostAttributeByCommodityIdAndAttributeId(commodityId, item.getId());
					if (commodityCostAttributes != null && commodityCostAttributes.size() > 0) {
						CommodityCostAttribute commodityCostAttribute = commodityCostAttributes.get(0);
						CommodityCostAttributeReq commodityCostAttributeReq = new CommodityCostAttributeReq();
						BeanUtils.copyProperties(commodityCostAttribute, commodityCostAttributeReq);
						commodityCostSingleReq.setCommodityCostAttributeReq(commodityCostAttributeReq);
					}
					commodityCostReqList.add(commodityCostSingleReq);
				});
				commodityDTO.setCommodityCostReqList(commodityCostReqList);
			}


			// 查询销售区域
			List<CommoditySalesArea> commoditySalesArea = commoditySalesAreaMapper.findCommoditySalesAreaByCommodityId(commodityId);
			log.info("【中旅商城--审批中心--根据ID查询商品明细--查询销售区域】,返参为:{}",JSON.toJSONString(commoditySalesArea));
			if(commoditySalesArea != null && commoditySalesArea.size() > 0){
				List<CommoditySalesAreaReq> commoditySalesAreaReqs = commodityDTO.getCommoditySalesAreaReq();
				for (CommoditySalesArea salesArea : commoditySalesArea) {
					CommoditySalesAreaReq commoditySalesAreaReq = new CommoditySalesAreaReq();
					BeanUtils.copyProperties(salesArea, commoditySalesAreaReq);
					commoditySalesAreaReqs.add(commoditySalesAreaReq);
				}
				commodityDTO.setCommoditySalesAreaReq(commoditySalesAreaReqs);
			}
		}


		// 根据商品ID，获取商品关键词数据，进行组装
		List<CommodityKeyWord> dbCommodityKeyWordList = keyWordMapper.findHtmlFiveCommodityKeyWordByCommodityId(commodityDTO.getId());
		log.info("【中旅商城--审批中心--根据ID查询商品明细--获取商品关键词数据进行组装】,返参为:{}",JSON.toJSONString(dbCommodityKeyWordList));
		List<CommodityKeyWordReq> commodityKeyWordReqList = new ArrayList<>();
		for (CommodityKeyWord dbKeyWord : dbCommodityKeyWordList) {
			CommodityKeyWordReq keyWordReq = new CommodityKeyWordReq();
			BeanUtils.copyProperties(dbKeyWord, keyWordReq);
			commodityKeyWordReqList.add(keyWordReq);
		}
		if (null != commodityKeyWordReqList && commodityKeyWordReqList.size() > 0) {
			commodityDTO.setCommodityKeyWordReqList(commodityKeyWordReqList);
		}

		// 根据商品ID，获取商品标签数据，进行组装
		List<CommodityLabel> dbCommodityLabelList = labelMapper.findHtmlFiveCommodityLabelByCommodityId(commodityDTO.getId());
		log.info("【中旅商城--审批中心--根据ID查询商品明细--获取商品标签数据进行组装】,返参为:{}",JSON.toJSONString(dbCommodityLabelList));
		List<CommodityLabelReq> commodityLabelReqList = new ArrayList<>();
		for (CommodityLabel dbLabel : dbCommodityLabelList) {
			CommodityLabelReq labelReq = new CommodityLabelReq();
			BeanUtils.copyProperties(dbLabel, labelReq);
			commodityLabelReqList.add(labelReq);

		}
		if (null != commodityLabelReqList && commodityLabelReqList.size() > 0) {
			commodityDTO.setCommodityLabelReqList(commodityLabelReqList);
		}

		// 根据商品ID，获取商品推荐数据，进行组装
		List<CommodityRecommend> dbCommodityRecommendList = recommendMapper.findHtmlFiveCommodityRecommendByCommodityId(commodityDTO.getId());
		List<CommodityRecommendReq> commodityRecommendReqList = new ArrayList<>();
		for (CommodityRecommend dbRecommend : dbCommodityRecommendList) {
			CommodityRecommendReq recommendReq = new CommodityRecommendReq();
			BeanUtils.copyProperties(dbRecommend, recommendReq);
			commodityRecommendReqList.add(recommendReq);

		}
		if (null != commodityRecommendReqList && commodityRecommendReqList.size() > 0) {
			commodityDTO.setCommodityRecommendReqList(commodityRecommendReqList);
		}

		// 根据商品ID，获取商品文件数据，进行组装
		List<CommodityFile> dbCommodityFileList = fileMapper.findHtmlFiveCommodityFileByCommodityId(commodityDTO.getId());
		log.info("【中旅商城--审批中心--根据ID查询商品明细--获取商品文件数据进行组装】,返参为:{}",JSON.toJSONString(dbCommodityFileList));
		List<CommodityFileReq> commodityFileReqList = new ArrayList<>();
		for (CommodityFile dbCommodityFile : dbCommodityFileList) {
			CommodityFileReq commodityFileReq = new CommodityFileReq();
			BeanUtils.copyProperties(dbCommodityFile, commodityFileReq);
			commodityFileReqList.add(commodityFileReq);
		}
		if (null != commodityFileReqList && commodityFileReqList.size() > 0) {
			commodityDTO.setCommodityFileReqList(commodityFileReqList);
		}

		// 组装单规格或多规格参数
		CommodityAttributeReq attributeReq = commodityDTO.getCommodityAttributeReq();
		log.info("【中旅商城--审批中心--根据ID查询商品明细--组装单规格或多规格参数】,入参为:{}",JSON.toJSONString(attributeReq));
		if (attributeReq != null && attributeReq.getSpecificationsType() != null && attributeReq.getSpecificationsType().intValue() == 1) {
			CommodityCostReq commodityCostReq = null;
			List<CommodityCost> dbCommodityCostList = costMapper.findHtmlFiveCommodityCostByCombinedIds(commodityDTO.getId(), attributeReq.getId(), null);
			if (null != dbCommodityCostList && dbCommodityCostList.size() > 0) {
				CommodityCost dbCommodityCost = dbCommodityCostList.get(0);
				commodityCostReq = new CommodityCostReq();
				BeanUtils.copyProperties(dbCommodityCost, commodityCostReq);
			}
			List<CommodityCostSingleReq> commodityCostSingleReqList = costMapper.findHtmlFiveCommodityCostAndAttributeByCombinedIds(commodityId, commodityCostReq.getCommodityAttributeId());
			if(commodityCostSingleReqList != null){
				for (CommodityCostSingleReq commodityCostSingleReq : commodityCostSingleReqList) {
					List<CommodityCostAttribute> commodityCostAttributes = costAttributeMapper.findCostAttributeByCommodityIdAndAttributeId(commodityId, commodityCostSingleReq.getCostAttributeId());
					if(commodityCostAttributes != null && commodityCostAttributes.size() > 0){
						CommodityCostAttributeReq commodityCostAttributeReq = new CommodityCostAttributeReq();
						BeanUtils.copyProperties(commodityCostAttributes.get(0), commodityCostAttributeReq);
						commodityCostSingleReq.setCommodityCostAttributeReq(commodityCostAttributeReq);
					}
				}
				commodityDTO.setCommodityCostReqList(commodityCostSingleReqList);
			}
			if (null != commodityCostReq) {
				commodityDTO.setCommodityCostReq(commodityCostReq);
			}
		} else {
			setMultiSpecificationRelatedParamInfo(commodityDTO, attributeReq);
		}
		String objStr = JSON.toJSONString(commodityDTO, SerializerFeature.WriteMapNullValue);
		if (StringUtils.isNotBlank(objStr)){
			objStr.replace("null","''");
			redisTemplate.opsForValue().set(keyName+commodityId, objStr,7, TimeUnit.DAYS);
		}
		log.info("【中旅商城--审批中心--根据ID查询商品明细--end】");
		return JSON.parseObject(objStr);
	}

	@Override
	public List<CommodityAllListDTO> findCommodityAllList(LocalDateTime lastNow) {
		// 过滤商品主表数据，根据主表数据去组装其他数据参数
		List<CommodityAllListDTO> commodityList = commodityMapper.findCommodityAllList(lastNow);
		if (CollectionUtils.isEmpty(commodityList)) {
			return null;
		}
		Map<Long, List<CommodityLabelDTO>> labelMap = null;
		Map<Long, List<CommodityFileDTO>> fileMap = null;
		Map<Long, List<CommodityKeyWordDTO>> keyWordMap = null;
		Map<Long, List<CommodityRecommendDTO>> recommendMap = null;

		List<Long> commodityIds = commodityList.stream().map(p -> p.getId()).collect(Collectors.toList());

		// 获取商品标签，并根据商品ID进行分组，用于后续的组装参数取值
		List<CommodityLabelDTO> labelList = labelMapper.findHtmlFiveCommodityLabelByCommodityIds(commodityIds);
		if (CollectionUtils.isNotEmpty(labelList)){
			labelMap = labelList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityId()));
		}

		// 获取商品文件，并根据商品ID进行分组，用于后续的组装参数取值
		List<CommodityFileDTO> fileList = fileMapper.findHtmlFiveCommodityFileByCommodityIds(commodityIds);
		if (CollectionUtils.isNotEmpty(fileList)){
			fileMap = fileList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityId()));
		}

		// 获取商品关键词，并根据商品ID进行分组，用于后续的组装参数取值
		List<CommodityKeyWordDTO>  keyWordList = keyWordMapper.findHtmlFiveCommodityKeyWordByCommodityIds(commodityIds);
		if (CollectionUtils.isNotEmpty(keyWordList)){
			keyWordMap = keyWordList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityId()));
		}

		// 获取商品推荐，并根据商品ID进行分组，用于后续的组装参数取值
		List<CommodityRecommendDTO> recommendList =recommendMapper.findHtmlFiveCommodityRecommendByCommodityIds(commodityIds);
		if (CollectionUtils.isNotEmpty(recommendList)){
			recommendMap = recommendList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityId()));
		}

		// 商品其他属性 一对一 查询赋值
		for (CommodityAllListDTO commodityAllList : commodityList) {
			Long commodityId = commodityAllList.getId();
			commodityAllList.setCommodityLabels(null == labelMap?null:labelMap.get(commodityId));
			commodityAllList.setCommodityFiles(null == fileMap?null:fileMap.get(commodityId));
			commodityAllList.setCommodityKeyWords(null == keyWordMap?null:keyWordMap.get(commodityId));
			commodityAllList.setCommodityRecommends(null == recommendMap?null:recommendMap.get(commodityId));
		}

		// 区分 单/多规格类型数据，因为组装的参数涉及表关联关系不一样
		Map<Integer, List<CommodityAllListDTO>> specificationsTypeMap = commodityList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityAttribute().getSpecificationsType()));
		List<CommodityAllListDTO> singles = specificationsTypeMap.get(1);
		List<CommodityAllListDTO> multiples = specificationsTypeMap.get(2);

		// 单规格参数组装
		if (CollectionUtils.isNotEmpty(singles)){
			Map<Long, List<CommodityCostDTO>> costs = getSingleCommodityCostMapByAttributeId(singles);
			for (CommodityAllListDTO singlesCommodityAllList:singles) {
				Long commodityAttributeId = singlesCommodityAllList.getCommodityAttribute().getId();
				List<CommodityCostDTO> temps = costs.get(commodityAttributeId);
				if (CollectionUtils.isNotEmpty(temps)){
					singlesCommodityAllList.setCommodityCosts(temps);
				}
			}
		}

		// 多规格参数组装
		if (CollectionUtils.isNotEmpty(multiples)) {
			Map<Long, List<CommodityCostAttributeDTO>> costAttributes = getMultipleCommodityCostAttributeMapByAttributeId(multiples);
			for (CommodityAllListDTO multipleCommodityAllList: multiples) {
				Long commodityAttributeId = multipleCommodityAllList.getCommodityAttribute().getId();
				List<CommodityCostAttributeDTO> temps = costAttributes.get(commodityAttributeId);
				if (CollectionUtils.isNotEmpty(temps)){
					multipleCommodityAllList.setCommodityCostAttributes(temps); // 讲商品成本属性设置到对应的商品对象
					// 获取商品成本属相对应的商品成本
					List<Long> costAttributeIds = temps.stream().map(p -> p.getId()).collect(Collectors.toList());
					List<CommodityCostDTO> resultCostList = EntityUtil.copyList(
							costMapper.selectList(
									new QueryWrapper<CommodityCost>().lambda()
											.eq(CommodityCost::getCommodityId,multipleCommodityAllList.getCommodityAttribute().getCommodityId())
											.eq(CommodityCost::getCommodityAttributeId, commodityAttributeId)
											.in(CommodityCost::getCostAttributeId, costAttributeIds)
											.eq(CommodityCost::getIsDeleted, IsDeletedCode.NO.getCode())

							), CommodityCostDTO.class);
					Map<Long, List<CommodityCostDTO>> costAttributeMap = resultCostList.stream().collect(Collectors.groupingBy(obj -> obj.getCostAttributeId()));

					for (CommodityCostAttributeDTO costAttribute: multipleCommodityAllList.getCommodityCostAttributes()) {
						Long costAttributeId = costAttribute.getId();
						List<CommodityCostDTO> commodityCost = costAttributeMap.get(costAttributeId);
						if (CollectionUtils.isNotEmpty(commodityCost)){
							costAttribute.setCommodityCost(commodityCost.get(0));
						}
					}
				}
			}
		}
		return commodityList;
	}

	@Override
	public JSONObject findCommodityAttributesListById(Long id) {
		JSONObject jsonObject = new JSONObject();
		// 1. 查询列表信息
		CommodityDTO2 commodityDTO = commodityMapper.findCommodityByCommodityId2(id);
		if(commodityDTO != null && commodityDTO.getCommoditySpecificationsNameId() != null){
			
		}
		return jsonObject;
	}

	/**
	 * 获取到所有的商品成本属性后，根据属性分组
	 * @param multiples 多规格商品集合
	 * @return 商品属性集合
	 */
	private Map<Long, List<CommodityCostAttributeDTO>> getMultipleCommodityCostAttributeMapByAttributeId(List<CommodityAllListDTO> multiples) {
		Map<Long, List<CommodityCostAttributeDTO>> result = null;
		List<Long> multipleCommodityIds = multiples.stream().map(p -> p.getId()).collect(Collectors.toList());
		List<CommodityCostAttributeDTO> costAttributeList= EntityUtil.copyList(
				costAttributeMapper.selectList(
						new QueryWrapper<CommodityCostAttribute>().lambda()
								.in(CommodityCostAttribute::getCommodityId,multipleCommodityIds)
								.eq(CommodityCostAttribute::getIsDeleted, IsDeletedCode.NO.getCode())

				), CommodityCostAttributeDTO.class);
		if (CollectionUtils.isNotEmpty(costAttributeList)){
			result = costAttributeList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityAttributeId()));
		}
		return result;
	}

	/**
	 * 获取到所有的商品成本后，根据商品属性分组
	 * @param singles 单规格商品集合
	 * @return 商品属性分组后的商品集合
	 */
	private Map<Long, List<CommodityCostDTO>> getSingleCommodityCostMapByAttributeId(List<CommodityAllListDTO> singles) {
		Map<Long, List<CommodityCostDTO>> result = null;
		List<Long> singleCommodityIds = singles.stream().map(p -> p.getId()).collect(Collectors.toList());
		List<CommodityCostDTO> resultList= EntityUtil.copyList(
				costMapper.selectList(
						new QueryWrapper<CommodityCost>().lambda()
								.in(CommodityCost::getCommodityId,singleCommodityIds)
								.eq(CommodityCost::getIsDeleted, IsDeletedCode.NO.getCode())

				), CommodityCostDTO.class);
		if (CollectionUtils.isNotEmpty(resultList)){
			result = resultList.stream().collect(Collectors.groupingBy(obj -> obj.getCommodityAttributeId()));
		}
		return result;
	}

	private void setMultiSpecificationRelatedParamInfo(CommodityDTO commodityDTO, CommodityAttributeReq attributeReq){
		List<CommodityCostSingleReq> dbCommodityCostSingleList = costMapper.findHtmlFiveCommodityCostAndAttributeByCombinedIds(commodityDTO.getId(), attributeReq.getId());
		if (dbCommodityCostSingleList != null){
			commodityDTO.setCommodityCostReqList(dbCommodityCostSingleList);
			List<Long> specificationsAttributeId = new ArrayList<>();
			for (CommodityCostSingleReq costSingleReq:dbCommodityCostSingleList) {
				CommodityCostAttributeReq costAttributeReq = costSingleReq.getCommodityCostAttributeReq();
				if (!specificationsAttributeId.contains(costAttributeReq.getSpecificationsAttributeOneId())){
					specificationsAttributeId.add(costAttributeReq.getSpecificationsAttributeOneId());
				}
				if (!specificationsAttributeId.contains(costAttributeReq.getSpecificationsAttributeTwoId())){
					specificationsAttributeId.add(costAttributeReq.getSpecificationsAttributeTwoId());
				}
			}
			List<ShoppingCommoditySpecificationsAttribute> specificationsAttributeList =  specificationsAttributeMapper.selectBatchIds(specificationsAttributeId);

			Long specificationsNameId = null;
			List<Long> specificationsId = new ArrayList<>();
			if (null != specificationsAttributeList && specificationsAttributeList.size() >0){
				specificationsNameId = specificationsAttributeList.get(0).getSpecificationsNameId();
				for (ShoppingCommoditySpecificationsAttribute specificationsAttribute:specificationsAttributeList) {
					if (!specificationsId.contains(specificationsAttribute.getSpecificationsId())){
						specificationsId.add(specificationsAttribute.getSpecificationsId());
					}
				}

				ShoppingCommoditySpecificationsName specificationsName = specificationsNameMapper.selectById(specificationsNameId);

				ShoppingCommoditySpecificationsNameReq specificationsNameReq = new ShoppingCommoditySpecificationsNameReq();
				BeanUtils.copyProperties(specificationsName, specificationsNameReq);

				List<ShoppingCommoditySpecifications> dbShoppingCommoditySpecificationsList= specificationsMapper.findHtmlFiveSpecificationsByNameId(specificationsNameId);

				List<ShoppingCommoditySpecificationsReq> specificationsReqs = new ArrayList<>();
				for (ShoppingCommoditySpecifications dbSpecifications:dbShoppingCommoditySpecificationsList) {
					ShoppingCommoditySpecificationsReq specificationsReq = new ShoppingCommoditySpecificationsReq();
					BeanUtils.copyProperties(dbSpecifications, specificationsReq);
					List<ShoppingCommoditySpecificationsAttributeReq> specificationsAttributeReqs = new ArrayList<>();
					for (ShoppingCommoditySpecificationsAttribute specificationsAttribute:specificationsAttributeList) {
						if (specificationsNameId.longValue() == specificationsAttribute.getSpecificationsNameId().longValue()
								&& (specificationsReq.getId().longValue() == specificationsAttribute.getSpecificationsId().longValue())){
							ShoppingCommoditySpecificationsAttributeReq specificationsAttributeReq = new ShoppingCommoditySpecificationsAttributeReq();
							BeanUtils.copyProperties(specificationsAttribute, specificationsAttributeReq);
							specificationsAttributeReqs.add(specificationsAttributeReq);
						}
					}
					specificationsReq.setShoppingCommoditySpecificationsAttributeReqList(specificationsAttributeReqs);
					specificationsReqs.add(specificationsReq);
				}
				specificationsNameReq.setShoppingCommoditySpecificationsReqList(specificationsReqs);
				commodityDTO.setShoppingCommoditySpecificationsNameReq(specificationsNameReq);
			}
		}
	}
}
